/** @file    Request.cpp
 *  @author  Alessandro Polo
 *  @version $Id: Request.cpp 2831 2010-08-02 22:15:14Z alex $
 *  @brief
 * File containing methods for the wosh::Request class.
 * The header for this class can be found in Request.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <core/Request.h>
 #include <core/ObjectAllocator.h>
 #include <core/List.h>
 #include <core/Response.h>


using namespace std;
namespace wosh {

 WOSH_REGISTER(wosh::Request, "wosh::MessageContentGeneric", 1.01, _static_Request)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

Request::Request()
	: MessageContentGeneric(), omitResponseFlag(false) {
 }

Request::Request( const std::string& method )
	: MessageContentGeneric( method ), omitResponseFlag(false) {
 }

Request::Request( const std::string& method, ISerializable* data )
	: MessageContentGeneric(method, data), omitResponseFlag(false) {
 }

Request::Request( const Request& m )
	: MessageContentGeneric(m), omitResponseFlag(m.omitResponseFlag) {
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Request::clear() {
	MessageContentGeneric::clear();
	this->omitResponseFlag = false;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////// OPERATORS

Request& Request::operator=(const Request& m) {
	if (this == &m) return *this; // same object?
	(MessageContentGeneric&)*this = (const MessageContentGeneric&)m;
	this->omitResponseFlag = m.omitResponseFlag;
	return *this;
 }

bool Request::operator==(const Request &other) const {
	return ( (const MessageContentGeneric&)*this == (const MessageContentGeneric&)other );
	return ( this->omitResponseFlag == other.omitResponseFlag );
 }

/////////////////////////////////////////////////////////////////////////////////////////////////// OPERATORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

Response* Request::replyResponse( WRESULT ReturnValue, ISerializable* data ) const {
	if ( this->omitResponseFlag ) return NULL;
	return new Response( this->contentContext, data, ReturnValue );
 }

Response* Request::replyResponse( WRESULT ReturnValue, const char* message ) const {
	if ( this->omitResponseFlag ) return NULL;
	if ( message == NULL )
		return new Response( this->contentContext, NULL, ReturnValue );
	return new Response( this->contentContext, new Variant(message), ReturnValue );
 }

Response* Request::replyResponse( WRESULT ReturnValue ) const {
	if ( this->omitResponseFlag ) return NULL;
	return new Response( this->contentContext, NULL, ReturnValue );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

const Variant& Request::getArgument() const {
	if ( this->content == NULL ) return Variant::EmptyVariant;
	if ( this->content->isKindOf<Variant>() ) {
		const Variant* data = this->content->as<Variant>();
		if ( data != NULL ) return *data;
	 }
	return Variant::EmptyVariant;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

int Request::extractArguments( Variant& arg0 ) const {
	if ( this->content == NULL ) return -1;
	if ( this->content->isKindOf<Variant>() ) {
		const Variant* data = this->content->as<Variant>();
		if ( data == NULL ) return -2;
		arg0 = *data;
		return 1;
	 }
	if ( this->content->isKindOf<List>() ) {
		const List* dl = dynamic_cast<const List*>(this->content);
		if ( dl == NULL ) return -2;
		if ( dl->size() == 0 ) return 0;
		const ISerializable* iSer = dl->itemAt(0);
		if ( iSer == NULL ) return -3;
		if ( !iSer->isKindOf<Variant>() ) return -4;
		const Variant* data = dynamic_cast<const Variant*>(iSer);
		if ( data == NULL ) return -4;
		arg0 = *data;
		return 1;
	 }
	arg0.set( this->content->toString() );
	return 0;
 }

int Request::extractArguments( Variant& arg0, Variant& arg1 ) const {
	if ( extractArguments(arg0) != 1 ) return 0;
	if ( this->content->isKindOf<List>() ) {
		const List* dl = dynamic_cast<const List*>(this->content);
		if ( dl == NULL ) return 1;
		if ( dl->size() < 2 ) return 1;
		const ISerializable* iSer = dl->itemAt(1);
		if ( iSer == NULL ) return -3;
		if ( !iSer->isKindOf<Variant>() ) return -4;
		const Variant* data = dynamic_cast<const Variant*>(iSer);
		if ( data == NULL ) return -4;
		arg1 = *data;
		return 2;
	 }
	return 1;
  }
 
int Request::extractArguments( Variant& arg0, Variant& arg1, Variant& arg2 ) const {
	if ( extractArguments(arg0, arg1) != 2 ) return 0;
	if ( this->content->isKindOf<List>() ) {
		const List* dl = dynamic_cast<const List*>(this->content);
		if ( dl == NULL ) return 1;
		if ( dl->size() < 3 ) return 1;
		const ISerializable* iSer = dl->itemAt(2);
		if ( iSer == NULL ) return -3;
		if ( !iSer->isKindOf<Variant>() ) return -4;
		const Variant* data = dynamic_cast<const Variant*>(iSer);
		if ( data == NULL ) return -4;
		arg2 = *data;
		return 2;
	 }
	return 1;
  }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

}; // namespace wosh
